package org.gl.rtsp;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * RTSP 请求的封装
 * @author Administrator
 *
 */
public class Request {

	// Parse method & uri
	public static final Pattern regexMethod = Pattern.compile(
			"(\\w+) (\\S+) RTSP", Pattern.CASE_INSENSITIVE);
	// Parse a request header
	private static final Pattern rexegHeader = Pattern.compile("(\\S+):(.+)",
			Pattern.CASE_INSENSITIVE);
	public int port;
	public String host;
	public String url;
	final static String KEY_TARGETURL = "c";
	final static String KEY_GATEWAY = "w";
	public String method;
	public final static String Method_PLAY = "PLAY";
	public final static String Method_DESCRIBE = "DESCRIBE";
	public static String version = " RTSP/1.0";
	public String uri;
	public URL mProxyUrl;
	public HashMap<String, String> headers = new HashMap<String, String>();
	public String gateway;

	/** Parse the method, uri & headers of a RTSP request */
	public static Request parseRequest(BufferedReader input)
			throws IOException, IllegalStateException, SocketException {
		Request request = new Request();
		String line;
		Matcher matcher;

		// Parsing request method & uri
		if ((line = input.readLine()) == null)
			throw new SocketException("Client disconnected");
		matcher = regexMethod.matcher(line);
		matcher.find();
		request.method = matcher.group(1);
		request.uri = matcher.group(2);

		// Parsing headers of the request
		while ((line = input.readLine()) != null && line.length() > 3) {
			matcher = rexegHeader.matcher(line);
			matcher.find();
			request.headers.put(matcher.group(1).toLowerCase(Locale.US),
					matcher.group(2));
		}
		if (line == null)
			throw new SocketException("Client disconnected");

		// It's not an error, it's just easier to follow what's happening in
		// logcat with the request in red

		return request;
	}

	public static Request parseRequestFirst(String line)
			throws IOException {
		System.out.println("firstline:" + line);
		Matcher matcher = regexMethod.matcher(line);
		matcher.find();
		Request request = new Request();
		try {
			request.method = matcher.group(1);
			request.uri = matcher.group(2);
			request.url= request.uri;
			System.out.println("uri:" + request.uri);
			System.out.println("method:" + request.method);
		} catch (IllegalStateException e) {
			System.err.println(line);
			throw e;
		}
		
		URL url = parseRTSPURL(request.uri);
		if (url == null) {
			throw new IllegalArgumentException();
		}
		
		request.host = url.host;
		request.port = url.getPort();

		String dsturl = url.getParam(KEY_TARGETURL);
		request.gateway = url.getParam(KEY_GATEWAY);
		System.out.println("dsturl:"+dsturl);
		// get the real IP and port from proxy url
		if (dsturl != null) {
			request.mProxyUrl = parseRTSPURL(dsturl);
		}

		return request;
	}

	private static org.gl.rtsp.URL parseRTSPURL(String str) {
		URL mURL = new URL(str);
		try {
			mURL.analysis();
			System.out.println(mURL.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mURL;
	}

}